Ext.define('Ssimgres.controller.Cohorte', {
	extend : 'Ext.app.Controller',
	views : ['cohorte.CohortesView', 'cohorte.CohorteView'],
	stores : ['Capacitaciones', 'Cohortes'],
	models : ['Cohorte'],
	refs : [{
		ref : 'cohortesView',
		selector : 'cohortesView'
	}, {
		ref : 'cohorteView',
		selector : 'cohorteView'
	}],
	init : function() {
		this.control({
			'cohortesView' : {
				itemclick : this.activarBotones
			},
			'cohortesView button[action=btnNuevo]' : {
				click : this.cohorteViewShow
			},
			'cohortesView button[action=btnEliminar]' : {
				click : this.eliminarTipo
			},
			'cohortesView button[action=btnEditar]' : {
				click : this.cohorteViewShowEditar
			},
			'cohorteView button[action=btnCancelar]' : {
				click : this.cohorteViewCancelar
			},
			'cohorteView button[action=btnGuardar]' : {
				click : this.cohorteViewGuardar
			},
		});
	},
	reset : function() {
		Ext.getCmp('cohortesView_btnEditar').setDisabled(true);
		Ext.getCmp('cohortesView_btnEliminar').setDisabled(true);
		this.getCohortesStore().load();
		this.getCohortesView().getSelectionModel().clearSelections();
	},
	cohorteViewShow : function() {
		var Cohorte = this.getCohorteModel();
		var cohorte = new Cohorte();
		this.getCapacitacionesStore().load();
		Ext.widget('cohorteView').show();
		var form = this.getCohorteView().down('form').getForm();
		form.loadRecord(cohorte);
	},
	cohorteViewShowEditar : function() {
		this.getCapacitacionesStore().load();
		Ext.widget('cohorteView').show();
		var form = this.getCohorteView().down('form').getForm();
		form.loadRecord(this.getCohortesView().getSelectionModel().getSelection()[0]);
	},
	activarBotones : function() {
		Ext.getCmp('cohortesView_btnEditar').setDisabled(false);
		Ext.getCmp('cohortesView_btnEliminar').setDisabled(false);
	},
	cohorteViewCancelar : function() {
		this.getCohorteView().close();
	},
	cohorteViewGuardar : function() {
		var form = this.getCohorteView().down('form').getForm();
		form.updateRecord();
		if (form.isValid()) {
			form.getRecord().save({
				success : function(operation) {
					Ext.Msg.show({
						title : 'Éxito',
						msg : operation.proxy.reader.jsonData["msg"],
						buttons : Ext.Msg.OK,
						icon : Ext.Msg.INFO
					});
					this.getCohorteView().close();
					this.reset();
				},
				failure : function(operation) {
					Ext.Msg.show({
						title : 'Error',
						msg : operation.proxy.reader.jsonData["msg"],
						buttons : Ext.Msg.OK,
						icon : Ext.Msg.ERROR
					});
				},
				scope : this
			});
		} else {
			Ext.Msg.show({
				title : 'Atención',
				msg : 'Verifica los campos del formulario',
				buttons : Ext.Msg.OK,
				icon : Ext.Msg.WARNING
			});
		}

	},
	eliminarTipo : function() {
		var record = this.getCohortesView().getSelectionModel().getSelection()[0];
		Ext.Msg.confirm('Eliminar registro', '¿Está seguro de la operación?', function(id, value) {
			if (id === 'yes') {
				record.destroy({
					success : function(operation) {
						Ext.Msg.show({
							title : 'Éxito',
							msg : operation.proxy.reader.jsonData["msg"],
							buttons : Ext.Msg.OK,
							icon : Ext.Msg.INFO
						});
						this.reset();
					},
					failure : function(operation) {
						Ext.Msg.show({
							title : 'Error',
							msg : operation.proxy.reader.jsonData["msg"],
							buttons : Ext.Msg.OK,
							icon : Ext.Msg.ERROR
						});
					},
					scope : this
				});
			}
		}, this);
	}
});
